package com.aaa.charge.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.aaa.charge.dao.*;
import com.aaa.charge.service.CareTuiService;
import com.aaa.charge.util.HttpClient;
import com.aaa.charge.vo.PostObjectVo;
import com.aaa.charge.vo.PostObjectVo1;
import com.aaa.entity.*;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.wxpay.sdk.WXPayUtil;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.*;

/**
 * @author pzs
 * @date 2023/3/30 18:51
 * @description CareTuiServiceImpl
 */
@Service
public class CareTuiServiceImpl implements CareTuiService {
    @Autowired
    private CareHistoryDao careHistoryDao;
    @Autowired
    private CareOrderDao careOrderDao;
    @Autowired
    private CareOrderItemDao careOrderItemDao;
    @Resource
    private BackfeeDao orderBackfeeDao;
    @Resource
    private BackfeeItemDao orderBackfeeItemDao;
    @Resource
    private ChargeItemDao chargeItemDao;


    @Value("${weixin.appid}")
    private String appId;
    @Value("${weixin.mch_id}")
    private String mchId;
    @Value("${weixin.api_key}")
    private String apiKey;

    @Override
    public Result<Map<String,Object>> findcareHistory(String regId){
        QueryWrapper<CareHistory> wrapper = new QueryWrapper<>();
        wrapper.eq("reg_id", regId);
        //new CareHistory实体获取一行数据
        CareHistory careHistory = careHistoryDao.selectOne(wrapper);
        if (careHistory==null){
            return new Result(200,"单号错误");
        }
        QueryWrapper<CareOrder> wrapper1=new QueryWrapper<>();
        wrapper1.eq("ch_id",careHistory.getChId());
        List<CareOrder> careOrders = careOrderDao.selectList(wrapper1);
        List<Map<String,Object>> list=new ArrayList<>();
        if(careOrders.size()>0){
            //遍历careOrders
            for (CareOrder careOrder : careOrders) {
                Map<String,Object> map1= BeanUtil.beanToMap(careOrder);
                QueryWrapper<CareOrderItem> wrapper2=new QueryWrapper<>();
                wrapper2.eq("status",1);
                wrapper2.eq("co_id",careOrder.getCoId());
                List<CareOrderItem> careOrderItems = careOrderItemDao.selectList(wrapper2);
                if (careOrderItems.size()!=0) {
                    //计算订单金额
                    Double sum = careOrderItems.stream()
                            .map(item -> item.getAmount()
                                    .doubleValue()).reduce((a, b) -> a + b).get();
                    map1.put("careOrderItems",careOrderItems);
                    map1.put("sum",sum);
                    list.add(map1);
                }
            }
        }
        if(list.isEmpty()){
            return new Result<>(200,"请输入为支付的订单Id");
        }else {
            HashMap<String, Object> map = new HashMap<>();
            map.put("careHistory", careHistory);
            map.put("list" , list);
            return new Result<>(200, "成功", map);
        }

    }
    @Transactional
    @Override
    public Result updateStatus(PostObjectVo postObjectVo) {
        String chId = postObjectVo.getOrderChargeDto().getChId();
        String backId="ODB1";
        Random random = new Random();
        for (int i = 0; i < 18; i++) {
            backId += String.valueOf(random.nextInt(10));
        }
        String orderId="";

        List<OrderChargeItem> orderChargeItem= careHistoryDao.selOrderId(postObjectVo.getOrderChargeDto().getRegId());
        for (OrderChargeItem chargeItem : orderChargeItem) {
             orderId = chargeItem.getOrderId();
        }
        for (PostObjectVo1 postObjectVo1 : postObjectVo.getOrderChargeItemDtoList()) {
            String itemId=postObjectVo1.getItemId();
            String coId = postObjectVo1.getCoId();
            String itemName = postObjectVo1.getItemName();
            BigDecimal itemPrice = BigDecimal.valueOf(Long.parseLong(postObjectVo1.getItemPrice()));
            BigDecimal itemAmount = BigDecimal.valueOf(Long.parseLong(postObjectVo1.getItemAmount()));
            String itemType = postObjectVo1.getItemType();
            Integer itemNum = Integer.valueOf(postObjectVo1.getItemNum());

            String status="2";

            OrderBackfeeItem orderBackfeeItem=new OrderBackfeeItem(itemId,coId,itemName,itemPrice,itemNum,itemAmount,backId,itemType,status);
            orderBackfeeItemDao.insert(orderBackfeeItem);
        }
        String backPlatformId="202";
        for (int i = 0; i < 10; i++) {
            backPlatformId += String.valueOf(random.nextInt(25));
        }
        //添加到收费列表
        String regId = postObjectVo.getOrderChargeDto().getRegId();
        String patientName = postObjectVo.getOrderChargeDto().getPatientName();
        BigDecimal backAmount = BigDecimal.valueOf(postObjectVo.getOrderChargeDto().getOrderAmount());
        String backType="0";
        String backStatus="1";
        LocalDateTime createTime = LocalDateTime.now();
        LocalDateTime backTime = LocalDateTime.now();
        OrderBackfee orderBackfee=new OrderBackfee(backId,backAmount,chId,regId,patientName,backStatus,backType, orderId,backPlatformId,backTime,createTime);
        orderBackfeeDao.insert(orderBackfee);
        //修改状态
        for (int i=0;i<postObjectVo.getOrderChargeItemDtoList().size();i++) {
            orderBackfeeDao.updateByItemId(postObjectVo.getOrderChargeItemDtoList().get(i).getItemId());
        }
        return new Result<>(200,"现金退费成功");
    }

    @Override
    public Result updateSta(PostObjectVo postObjectVo) {
        String chId = postObjectVo.getOrderChargeDto().getChId();
        String backId="ODB1";
        Random random = new Random();
        for (int i = 0; i < 18; i++) {
            backId += String.valueOf(random.nextInt(10));
        }
        String orderId="";

        List<OrderChargeItem> orderChargeItem= careHistoryDao.selOrderId(postObjectVo.getOrderChargeDto().getRegId());
        for (OrderChargeItem chargeItem : orderChargeItem) {
            orderId = chargeItem.getOrderId();
        }
        for (PostObjectVo1 postObjectVo1 : postObjectVo.getOrderChargeItemDtoList()) {
            String itemId=postObjectVo1.getItemId();
            String coId = postObjectVo1.getCoId();
            String itemName = postObjectVo1.getItemName();
            BigDecimal itemPrice = BigDecimal.valueOf(Long.parseLong(postObjectVo1.getItemPrice()));
            BigDecimal itemAmount = BigDecimal.valueOf(Long.parseLong(postObjectVo1.getItemAmount()));
            String itemType = postObjectVo1.getItemType();
            Integer itemNum = Integer.valueOf(postObjectVo1.getItemNum());

            String status="2";

            OrderBackfeeItem orderBackfeeItem=new OrderBackfeeItem(itemId,coId,itemName,itemPrice,itemNum,itemAmount,backId,itemType,status);
            orderBackfeeItemDao.insert(orderBackfeeItem);
        }
        String backPlatformId="202";
        for (int i = 0; i < 10; i++) {
            backPlatformId += String.valueOf(random.nextInt(25));
        }
        //添加到收费列表
        String regId = postObjectVo.getOrderChargeDto().getRegId();
        String patientName = postObjectVo.getOrderChargeDto().getPatientName();
        BigDecimal backAmount = BigDecimal.valueOf(postObjectVo.getOrderChargeDto().getOrderAmount());
        String backType="1";
        String backStatus="1";
        LocalDateTime createTime = LocalDateTime.now();
        LocalDateTime backTime = LocalDateTime.now();
        OrderBackfee orderBackfee=new OrderBackfee(backId,backAmount,chId,regId,patientName,backStatus,backType, orderId,backPlatformId,backTime,createTime);
        orderBackfeeDao.insert(orderBackfee);
        //修改状态
        for (int i=0;i<postObjectVo.getOrderChargeItemDtoList().size();i++) {
            orderBackfeeDao.updateByItemId(postObjectVo.getOrderChargeItemDtoList().get(i).getItemId());
        }
        return new Result<>(200,"支付宝退费成功");
    }
}
